package cn.b504.qpcr.config;

import net.sf.jni4net.Bridge;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContext;
import java.io.File;
import java.io.IOException;

@Component
public class Jni4net implements InitializingBean {
  private static final String DLL_FILE_NAME = "Azure2Java.j4n.dll";
  private final ServletContext servletContext;

  public Jni4net(ServletContext servletContext) {
    this.servletContext = servletContext;
  }

  private void init() throws IOException {
    Bridge.init();

    String webRootPath = servletContext.getRealPath("");
    File file = new File(webRootPath + "WEB-INF" + File.separator + "lib", DLL_FILE_NAME);

    if (file.exists() && Bridge.isRegistered()) {
      Bridge.LoadAndRegisterAssemblyFrom(file);
    }
  }

  @Override
  public void afterPropertiesSet() throws IOException {
    init();
  }
}
